Asset Management
Asset Management Managing your assets is very easy. When you first create your project using the OpenFL command-line, the folder will contain an XML file (in which to configure the project when it is built/tested) and two folders: ''src'' and ''assets''. The ''Assets ''folder is used to contain all of the necessary assets for your project. OpenFL has native support for multiple assets. In the XML file, you can specify renames, includes and conditionals for your asset folders. The ''rename ''attribute allows you to shorten the access path when writing code. This does not apply in the XML file itself. Instead of writing "assets/img" each time to access files in the "img" folder, instead we can omit the "assets/" part as in the example above. The ''include ''attribute allows you to specify which files should be included when the project is built. This is particularly useful when combined with the ''if ''and ''unless ''conditional attributes. The above example provides us with an easy way to build a project whenever we decide to use HTML5 as our target. Because embedded fonts support certain browsers (given their format) we need to include several different types. In the example, the first line specifies that '''unless '''we are compiling to HTML5 (such as windows or neko), we will only include ''ttf ''formats into the build. If the target '''is '''HTML5, then use all other formats including ttf. When using any function in the ''Assets'' class to acquire local resources, it always uses relative pathing. Consequently, any resources you wish to load must be within the project's root directory. In Flash, attempting to access the file system of a users computer directly (using for example "C:\") will result in a ''SecurityError''. Images OpenFL supports the following image formats: *.png, *.jpg, and *.bmp To use an image asset, the following code will return a BitmapData object. Assets.getBitmapData("img/myImage.png"); Fonts OpenFL supports ''ttf ''in all targets, but HTML5 also supports the following: *.woff, *.woff2, *.svg and *.eot To use a font in-game, the following code will return a Font object. Assets.getFont("font/myFont.ttf"); Sounds Sounds have a specific attribute in the XML file, namely ''type. ''This attribute allows you to specify what type of sound you are playing. If it's a long music file, the sound type should be "audio", and if it is for effect (or SFX), then it should be "sound". The above is example XML code that specifies that audio can be played as either sounds or as audio. The major difference is that OpenFL automatically assumes "audio" to be MP3 or OGG files. Using: Assets.getMusic("audio/mymusic.mp3"); Also automatically assumes that the file being loaded is an MP3 or OGG file, but still returns the same Sound object as: Assets.getSound("sfx/mysound.wav"); Which assumes the file to be WAV. Another (although minor) difference is that (unless using HTML5, which does not use a cache) the ''getMusic'' function does not use the local cache, and instead uses the Lime cache whichever the target, whereas ''getSound ''uses local cache.[https://github.com/openfl/openfl/blob/master/openfl/Assets.hx#L250-L343 Difference between getMusic and getSound function.] Consequently, the ''getSound ''function'' ''may have some minor performance benefits. Text You can use raw text resources in the form of XML, JSON or TXT files, which returns a String to be used directly into your projects. This is most useful with XML[http://api.haxe.org/Xml.html XML API] and JSON[http://docs.openfl.org/haxe/Json.html JSON API] in conjunction with their respective API's: haxe.Json //for JSON parsing //Example JSON parsing var parsedJson:Dynamic = Json.parse(Assets.getText("data/myGame.json")); haxe.Xml //for XML parsing //Example XML parsing var parsedXml:Xml = Xml.parse(Assets.getText("data/myGame.xml")); This is particularly useful when loading game-specific content and parsing it accordingly into the game. It can help organisation and keep data and code independent of each other. Bytes You can load bytes to create a buffer. A ''buffer ''is just computer jargon for working with binary data which is not otherwise recognisable by standard means. Technically, everything in a computer must be loaded using a buffer, because nothing we type into a computer a computer does not understand until translated into binary. This is where a ''buffer ''becomes useful, because we can handle the exact numeric positions and data and load them accordingly. However, this is a very advanced form of computing and a tutorial for creating and using buffers do not belong here. However, since it exists in the API, a quick introduction shouldn't hurt anyone, right? To load bytes, we use the following function in the Assets API: Assets.getBytes("binaries/mydata.dat"); Now let's assume that ''mydata.dat ''contains the following information written in the following order: 3852 My Quest Title A description for the quest. Data can be written to a stream which can be written/read to/from the hard disk. Every time we load a file, the executing application will load a buffer and "stream" the content into the buffer. The buffer is a series of bytes which contain positions and values for all the data in the stream. Let's say we want to acquire the first value in the stream, so the following example (not tested) should work: var myData:ByteArray = Assets.getBytes("binaries/mydata.dat"); trace(myData.readInt()); //Outputs 3852 When using any read function in the ByteArray, the position is automatically changed to the start of the next value. If the Bytes you are loading has been compressed, you will need to use the same compression algorithm used when it was compressed to decompress and read the contents. Using the following function will tell the ByteArray (internally) to use the given algorithm: myData.uncompress(CompressionAlgorithm.GZIP); If you wish to compress assets into your game, using the ''getBytes ''function in the Assets API is one way to access them, using whichever algorithm you desire. OpenFL natively supports GZIP, LZMA, ZLIB and Deflate.[http://docs.openfl.org/lime/utils/CompressionAlgorithm.html OpenFL compression algorithms] References